package OT.shoufouni.serverThreads;

import android.util.Log;
import OT.shoufouni.gpsMaps.findRoute;

public class serverUpdateFriendLocationQuaryThread extends Thread {
		private final findRoute callerActivity;
	   private final String emailText;
	   private String myEmail;
	 
	   public serverUpdateFriendLocationQuaryThread(findRoute callerActivity, String email, String myEmail) {
		   this.callerActivity = callerActivity;
		   this.emailText = email;
		   this.myEmail = myEmail;
	    }
	   public void run() {
		   while (true){
			   // update the friend location in a time
			   try {
				sleep(100000);
			} catch (InterruptedException e) {
				Log.e("serverUpdateFriendLocationQuaryThread ",e.getMessage());
			}
			// connect to server
			serverLocateFriendQuaryThread locateFriends = new serverLocateFriendQuaryThread(callerActivity, emailText,myEmail);
			locateFriends.start();
		   }
	   }	
}

















/*   private final findRoute callerActivity;
   private final String idToLocate;
   serverUpdateFriendLocationQuaryThread(findRoute callerActivity, String name, String myNAme) {
      this.callerActivity = callerActivity;
      this.idToLocate = name;
    }
   public void run() {
	   myLocation receivedData = httpTranslateGet(idToLocate);
	   callerActivity.responseDisplayFriendLocation(receivedData);
   }
   private  myLocation httpTranslateGet(String idToLocate) {
	   String requestUrl;
	   StringBuilder builder = new StringBuilder();
	   myLocation locations = null;
       try {
    	   String[] temp= idToLocate.split(" ");
           Log.d("serverUpdateFriendLocationQuaryThread ",temp[0]);

    	   requestUrl = "http://10.0.2.2:8080/serverRestSide2/findFriend?userName="+temp[0];
    	   try {
               URL url = new URL(requestUrl.toString());
               BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
               String inputLine = null;
               Log.d("serverUpdateFriendLocationQuaryThread ","-----RESPONSE START-----" +" id="+ Thread.currentThread().getId());
               
               while ((inputLine = in.readLine()) != null) {
            	   //builder.append(inputLine);
            	   if ("BAD".equals(inputLine)){
            		   return null;
            	   }
            	   String [] tempString=inputLine.split("\\,");
            	   locations = new myLocation(Double.parseDouble(tempString[0]),Double.parseDouble(tempString[1]));
                   Log.d("serverUpdateFriendLocationQuaryThread ",locations.getX()+","+locations.getY());

               }
               in.close();
               Log.d("serverUpdateFriendLocationQuaryThread ",builder.toString());
               Log.d("serverUpdateFriendLocationQuaryThread ","-----RESPONSE END-----" +" id="+ Thread.currentThread().getId());
               
               
           } catch (IOException e) {
        	   Log.e("serverUpdateFriendLocationQuaryThread ",e.getMessage());
               //e.printStackTrace();
               return null;
           }
       }finally{
    	   
    	   //return builder.toString();
       }
       //return Double.parseDouble(builder.toString());
      // return locations.toArray(new Double[locations.size()]);
       return locations;

   	}
   
}*/

